package com.thp.bigdata.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.junit.Test;

public class SaleAction {
	
	
	
	
	@Test
	public void saleByBossSelf() {
		IBoss boss = new Boss();
		System.out.println("老板自营!");
		int money = boss.yifu("xxl");
		System.out.println("衣服的成交价: " + money);
		
	}
	
	
	public static IBoss getProxyBoss(final int discountCoupon) {
		Object proxedObj = Proxy.newProxyInstance(Boss.class.getClassLoader(), new Class[] {IBoss.class}, new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				Integer returnValue = (Integer) method.invoke(new Boss(), args);  // 调用原始对象以后的返回值
				return returnValue - discountCoupon;
			}
		});
		return (IBoss) proxedObj;
	}
	
	
	@Test
	public void saleByProxy() {
		IBoss proxyBoss = SaleAction.getProxyBoss(20);
		System.out.println("代理经营!");
		int money = proxyBoss.yifu("xxl");  // 
		System.out.println("衣服的成交价 : " + money);
	}
	
	
	
	
	
	
}
